<?php
/**
 * Created by PhpStorm.
 * User: lijie
 * Date: 2018/10/7
 * Time: 9:01
 */
namespace app\reserve\controller;

use Admin\Controller\qrstr;
use think\cache\driver\Redis;
use think\Controller;
use app\reserve\model\Orderwash as OrderModel;
use think\Db;
use think\Request;

class Orderwash extends Controller{

    private $order;

    public function __construct()
    {
        $this->order = new OrderModel();
    }
    /**
     * Notes:  添加
     * Create Date: 2018/10/7
     * Create by :lijie
     */
    public function addOrderwash(Request $request)
    {
        $param = $request->post();
        $vali  = $this->validate($param,"Orderwash.addOrderwash");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        // 一卡通卡号
        $param['onecard']       = Db::name('onecard')->where('u_id',$param['u_id'])->value("cardnumber");
        $param['wallet']       = Db::name('onecard')->where('u_id',$param['u_id'])->value("wallet");
        // 洗车产品信息
        $product                = Db::name('washproduct')->where('id',$param['washpro_id'])->field("name,description,oldprice,nowprice")->find();
        $param['pro_name']      = $product['name'];
        $param['pro_descrip']   = $product['description'];
        $param['discounts_price'] = $product['oldprice'];
        $param['discounts_o_price'] = $product['nowprice'];
        if($param['discounts_o_price'] > $param["wallet"]){
            return apiResponse(201,'您的余额不足请您充值一卡通',"");
        }
        // 支付类型
        $param['pay_type']      = 1;
        // 洗车商户信息
        $wash                   = Db::name('washcash')->where('washid',$param['wash_id'])->field("allowradio,a_id")->find();
        $param['realget']       = $param['pay_price'] * $wash['allowradio']/100;
        $param['a_id']          = $wash['a_id'];
        // 订单号
        $param['ordernumber']   = "ZXCYZ".date("YmdHis",time()).mt_rand(100,999);
        $res['res']             = $this->order->addOrder($param);
        if($res){
            // 存入redis缓存
            $option = [
                'host'       => '127.0.0.1',
                'port'       =>  6370,
                'password'   => 'dev2018',
                'select'     => 0,
                'timeout'    => 0,
                'expire'     => 300,
                'persistent' => false,
                'prefix'     => '',
            ];
            $redis = new Redis($option);
            $code = mt_rand(100000,999999);
            $key  = "WashOrder".$param['wash_id'].$code;
            $redis->set($key,$param['ordernumber']);
            // 6位随机数
            $res['code'] = $code;
            return apiResponse(200,'添加成功',$res);
        }else {
            return apiResponse(201,"添加失败",$res);
        }
    }

    /**
     * Notes:  删除
     * Create Date: 2018/10/7
     * Create by :lijie
     */
    public function delOrderwash()
    {
        $id = input('post.id');
        if(empty($id)){
            return apiResponse(201,'id不能为空','');
        }
        //
        $res = $this->order->delOrder($id);
        if($res){
            return apiResponse(200,'删除成功',$res);
        }else {
            return apiResponse(201,"删除失败",$res);
        }
    }

    /**
     * Notes:  编辑
     * Create Date: 2018/10/7
     * Create by :lijie
     */
    public function editOrderwash(Request $request)
    {
        $param = $request->post();
        $vali  = $this->validate($param,"Orderwash.editOrderwash");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        //
        $where['id']     = $param['id'];
        $where['status'] = array("NEQ",9);
        $res = $this->order->editOrder($param,$where);
        if($res){
            return apiResponse(200,'编辑成功',$res);
        }else {
            return apiResponse(201,"编辑失败",$res);
        }
    }

    /**
     * Notes:  列表
     * Create Date: 2018/10/7
     * Create by :lijie
     */
    public function selectOrderwash(Request $request)
    {
        $param = $request->post();
        $vali  = $this->validate($param,"Orderwash.selectOrderwash");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        // 用户筛选
        if (!empty($param['u_id'])){
            $where['o.u_id'] = $param['u_id'];
        }
        //  商户筛选
        if(!empty($param['a_id'])){
            $where['o.a_id'] = $param['a_id'];
        }
        // 洗车商户筛选
        if(!empty($param['wash_id'])){
            $where['o.wash_id'] = $param['wash_id'];
        }
        // 订单编号查询
        if(!empty($param['ordernumber'])){
            $where['o.ordernumber'] = $param['ordernumber'];
        }
        // 支付方式筛选
        if(!empty($param['pay_type'])){
            $where['o.pay_type'] = $param['pay_type'];
        }
        // 状态筛选
        $where['o.status'] = array("NEQ",9);
        if(isset($param['status']) && $param['status']!=""){
            $where['o.status'] = $param['status'];
        }
        // 时间筛选
        if(!empty($param['start']) && !empty($param['end'])){
            $start = strtotime($param['start']." 00:00:00");
            $end   = strtotime($param['end']  ." 23:59:59");
            $where['o.ctime'] = array(array("EGT",$start),array("ELT",$end),'and');
        }
        $res['list']  = $this->order->selectOrder($where,$param['pagenum'],$param['listNum']);
        $res['total'] = $this->order->alias("o")->where($where)->count();
        return apiResponse(200,'获取成功',$res);
    }

    /**
     * Notes:  详情
     * Create Date: 2018/10/7
     * Create by :lijie
     */
    public function findOrderwash()
    {
        $id = input('post.id');
        if(empty($id)){
            return apiResponse(201,"id不能为空",'');
        }
        //
        $where['o.id']    = $id;
        $where['o.status']= array("NEQ",9);
        $res = $this->order->findOrder($where);
        if($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,"获取失败",$res);
        }
    }
}